package com.qiantu.phone.ui.uiutil;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.qiantu.api.db.LLDeviceCustomDBManager;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.ControlDataFloorHeatingBean;
import com.qiantu.api.entity.ControlDataFreshAirBean;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.DeviceCustomKeyBean;
import com.qiantu.api.entity.LinkageNewBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.api.entity.ControlDataAirConditionerBean;
import com.qiantu.api.entity.DeviceModeValue;
import com.qiantu.phone.ui.activity.BrightnessSettingActivity;
import com.qiantu.phone.ui.activity.BrightnessSettingActivityNew;
import com.qiantu.phone.ui.activity.ColorTemperatureSettingActivity;
import com.qiantu.phone.ui.activity.SelectCurtainsStatusActivity;
import com.qiantu.phone.ui.activity.SelectDeviceExhaustWindSpeedActivity;
import com.qiantu.phone.ui.activity.SelectDeviceModeActivity;
import com.qiantu.phone.ui.activity.SelectDeviceModeActivityNew;
import com.qiantu.phone.ui.activity.SelectDeviceWindDirectionActivity;
import com.qiantu.phone.ui.activity.SelectDeviceWindDirectionActivityNew;
import com.qiantu.phone.ui.activity.SelectDeviceWindSpeedActivity;
import com.qiantu.phone.ui.activity.SelectDeviceWindSpeedActivityNew;
import com.qiantu.phone.ui.activity.SwitchStatusActivity;
import com.qiantu.phone.ui.activity.SwitchStatusActivityNew;
import com.qiantu.phone.ui.fragment.device.AirConditionerFragment;
import com.qiantu.phone.ui.fragment.device.BFCurtainFragment;
import com.qiantu.phone.ui.fragment.device.BaseDeviceFragment;
import com.qiantu.phone.ui.fragment.device.BlindCurtainFragment;
import com.qiantu.phone.ui.fragment.device.ColorDeviceFragment;
import com.qiantu.phone.ui.fragment.device.ColorTemperatureFragment;
import com.qiantu.phone.ui.fragment.device.DimmingFragment;
import com.qiantu.phone.ui.fragment.device.DoorBellFragment;
import com.qiantu.phone.ui.fragment.device.EightKeyFragment;
import com.qiantu.phone.ui.fragment.device.FloorHeatingFragment;
import com.qiantu.phone.ui.fragment.device.FourKeyFragment;
import com.qiantu.phone.ui.fragment.device.FreshAirFragment;
import com.qiantu.phone.ui.fragment.device.KHCurtainFragment;
import com.qiantu.phone.ui.fragment.device.MusicFragment;
import com.qiantu.phone.ui.fragment.device.NoSuchFragment;
import com.qiantu.phone.ui.fragment.device.OneKeyFragment;
import com.qiantu.phone.ui.fragment.device.RollerShutterFragment;
import com.qiantu.phone.ui.fragment.device.SequencerFragment;
import com.qiantu.phone.ui.fragment.device.SixKeyFragment;
import com.qiantu.phone.ui.fragment.device.SwitchFragment;
import com.qiantu.api.utils.DeviceInfoParseUtil;
import com.qiantu.phone.ui.fragment.device.ThreeKeyFragment;
import com.qiantu.phone.ui.fragment.device.TwoKeyFragment;
import com.qiantu.phone.ui.fragment.device.UniversalAirFragment;
import com.qiantu.phone.ui.fragment.device.UniversalDeviceFragment;
import com.qiantu.phone.ui.fragment.device.WirelessInfraredFragment;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName LLDeviceUtil
 * @Description TODO
 * @Author ShiJunqiang
 * @Date 2021/12/9 11:25
 * @Version 1.0
 */
public class LLDeviceUtil {
    public final static int CURTAIN_OPEN = 2;
    public final static int CURTAIN_CLOSE = 1;
    public final static String POWER_ON = "1";
    public final static String POWER_OFF = "0";

    public static final String STATUS = "status";

    public static final String LIGHT = "light";
    public static final String KELVIN = "kelvin";
    public static final String RED = "red";
    public static final String BLUE = "blue";
    public static final String GREEN = "green";
    public static final String COLOR_VALUE = "colorValue";
    public static final String MODE = "mode";
    public static final String WIND_SPEED = "windspeed";
    public static final String EXHAUST_SPEED = "exhaustspeed";
    public static final String WIND_DIRECTION = "winddirection";
    public static final String TEMPERATURE = "temperature";
    public static final String STROKE_STATE = "strokestate";
    public static final String STROKE_PERCENTAGE = "strokepercentage";
    public static final String AMGLE = "angle";
    public static final String POWER = "power";
    public static final String IR_CTRL = "irctrl";
    public static final String WIRELESS_CTRL_433 = "wirelessctrl_433";
    public static final String WIRELESS_CTRL_315 = "wirelessctrl_315";
    public static final String LOCK = "lock";
    public static final String VOLUME = "volume";
    public static final String PLAYSTATE = "playstate";
    public static final String PROGRESS = "progress";
    public static final String PLAYMODE = "playmode";

    public static final String SONGINFO = "songinfo";
    public static final String UNIVERSAL = "universal";

    public static String getValueStr(String type, String deviceType, String attribute, String value, String deviceSerialNo) {
        if (LIGHT.equals(attribute.toLowerCase())
                || STROKE_PERCENTAGE.equals(attribute.toLowerCase()) || VOLUME.equals(attribute)) {
            return value + "%";
        } else if(AMGLE.equals(attribute)){
            return value + "°";
        }else if (KELVIN.equals(attribute)) {
            return value + "K";
        } else if (MODE.equals(attribute)) {
            if (DeviceMainType.AIRCONDITIONER.stringType.equals(type)) {
                return getAirConditionerModeNameByDeviceType(deviceType, attribute, value);
            } else if (DeviceMainType.FRESHAIR.stringType.equals(type)) {
                return getFreshAirModeNameByDeviceType(deviceType, attribute, value);
            } else if (DeviceMainType.FLOORHEATING.stringType.equals(type)) {
                return getFloorHeatingModeNameByDeviceType(deviceType, attribute, value);
            }
            return "";
        } else if (TEMPERATURE.equals(attribute)) {
            return value + "℃";
        } else if (WIND_SPEED.equals(attribute)) {
            if (DeviceMainType.AIRCONDITIONER.stringType.equals(type)) {
                return getAirConditionerModeNameByDeviceType(deviceType, attribute, value);
            } else if (DeviceMainType.FRESHAIR.stringType.equals(type)) {
                return getFreshAirModeNameByDeviceType(deviceType, attribute, value);
            }
            return "";
        } else if (WIND_DIRECTION.equals(attribute)) {
            if (DeviceMainType.AIRCONDITIONER.stringType.equals(type)) {
                return getAirConditionerModeNameByDeviceType(deviceType, attribute, value);
            } else if (DeviceMainType.FRESHAIR.stringType.equals(type)) {
                return getFreshAirModeNameByDeviceType(deviceType, attribute, value);
            }
            return "";
        } else if (POWER.equals(attribute)) {
            return POWER_ON.equals(value) ? getString(R.string.switch_on) : getString(R.string.switch_off);
        } else if (EXHAUST_SPEED.equals(attribute)) {
            return getFreshAirModeNameByDeviceType(deviceType, attribute, value);
        } else if (IR_CTRL.equals(attribute)
                || WIRELESS_CTRL_433.equals(attribute)
                || WIRELESS_CTRL_315.equals(attribute)) {
            if (DeviceType.INFRARED_KEY_1.stringType.equals(deviceType) ||
                    DeviceType.WIRELESS_315_KEY_1.stringType.equals(deviceType) ||
                    DeviceType.WIRELESS_433_KEY_1.stringType.equals(deviceType)) {
                return getString(R.string.on_off);
            } else if (DeviceType.INFRARED_KEY_2.stringType.equals(deviceType) ||
                    DeviceType.WIRELESS_315_KEY_2.stringType.equals(deviceType) ||
                    DeviceType.WIRELESS_433_KEY_2.stringType.equals(deviceType)) {
                switch (value){
                    case "1":
                        return getString(R.string.On_Englisth);
                    case "2":
                        return getString(R.string.Off_Englisth);
                }
            } else if (DeviceType.INFRARED_KEY_3.stringType.equals(deviceType) ||
                    DeviceType.WIRELESS_315_KEY_3.stringType.equals(deviceType) ||
                    DeviceType.WIRELESS_433_KEY_3.stringType.equals(deviceType)) {
                return getDeviceCustomKeys(deviceSerialNo, value);
            } else if (DeviceType.INFRARED_KEY_7.stringType.equals(deviceType) ||
                    DeviceType.WIRELESS_315_KEY_7.stringType.equals(deviceType) ||
                    DeviceType.WIRELESS_433_KEY_7.stringType.equals(deviceType)) {
                switch (value){
                    case "1":
                        return getString(R.string.on_off);
                    case "2":
                        return getString(R.string.Binging_Operation_More);
                    case "3":
                        return getString(R.string.Binging_Operation_Up);
                    case "4":
                        return getString(R.string.Binging_Operation_Right);
                    case "5":
                        return getString(R.string.Binging_Operation_Down);
                    case "6":
                        return getString(R.string.Binging_Operation_Left);
                    case "7":
                        return getString(R.string.Binging_Operation_OK);
                }
            } else if (DeviceType.INFRARED_KEY_9.stringType.equals(deviceType) ||
                    DeviceType.WIRELESS_315_KEY_9.stringType.equals(deviceType) ||
                    DeviceType.WIRELESS_433_KEY_9.stringType.equals(deviceType)) {
                switch (value) {
                    case "1":
                        return getString(R.string.on_off);
                    default:
                        return getDeviceCustomKeys(deviceSerialNo, value);
                }
            }
            return "";
        }else if (PLAYSTATE.equals(attribute)){
            return POWER_ON.equals(value) ? getString(R.string.play_music) : getString(R.string.stop_music);
        }else if(PROGRESS.equals(attribute)){
            return value;
        }else if(PLAYMODE.equals(attribute)){
            switch (value){
                case "2":
                    return getString(R.string.single_cycle);
                case "3":
                    return getString(R.string.order_play);
                case "5":
                    return getString(R.string.random_play);
                case "6":
                    return getString(R.string.repeat_all);
            }
        }else if(SONGINFO.equals(attribute)){
            try {
                JSONObject jsonObject=new JSONObject(value);
                String songname = jsonObject.getString("songname");
                return songname;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }else if(COLOR_VALUE.equals(attribute)){
            try {
                JSONObject jsonObject=new JSONObject(value);
                String color = "RGB:"+jsonObject.getString("red")+":"+jsonObject.getString("green") +":"+jsonObject.getString("blue");
                return color;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    private static String getDeviceCustomKeys(String deviceSerialNo, String keyIndex){
        List<DeviceCustomKeyBean> beans = LLDeviceCustomDBManager.getInstance(AppApplication.getInstance()).getDataByDeviceSerialNo(deviceSerialNo);
        String keyName = AppApplication.getInstance().getResources().getString(R.string.custom_name);;
            for (DeviceCustomKeyBean keyData : beans) {
                if(keyData.getKeyIndex().equals(keyIndex)){
                    keyName = keyData.getKeyName();
                    break;
                }
            }

        return keyName;
    }

    private static String getAirConditionerModeNameByDeviceType(String deviceType, String key, String value) {
        ControlDataAirConditionerBean.ControlData airConditionerInfoControlData = null;
        if (!TextUtils.isEmpty(deviceType)) {
            ControlDataAirConditionerBean airConditionerInfo = DeviceInfoParseUtil.parseData(AppApplication.getInstance(), deviceType, ControlDataAirConditionerBean.class);
            airConditionerInfoControlData = airConditionerInfo.getControlData();
        }
        List<DeviceModeValue> valueBeans = null;
        switch (key) {
            case MODE:
                if (airConditionerInfoControlData == null) {
                    return AppApplication.getInstance().getString(R.string.mode);
                }
                valueBeans = airConditionerInfoControlData.getMode();
                break;
            case WIND_SPEED:
                if (airConditionerInfoControlData == null) {
                    return AppApplication.getInstance().getString(R.string.wind_speed);
                }
                valueBeans = airConditionerInfoControlData.getWindspeed();
                break;
            case WIND_DIRECTION:
                if (airConditionerInfoControlData == null) {
                    return AppApplication.getInstance().getString(R.string.wind_direction);
                }
                valueBeans = airConditionerInfoControlData.getWinddirection();
                break;
        }
        if (valueBeans == null) {
            return null;
        }
        for (DeviceModeValue valueBean : valueBeans) {
            if ((valueBean.getValue() + "").equals(value)) {
                int nameRes = AppApplication.getInstance().getResources().getIdentifier(valueBean.getName(), "string", AppApplication.getInstance().getPackageName());
                if(nameRes == 0){
                    return valueBean.getName();
                }else{
                    return AppApplication.getInstance().getString(nameRes);
                }
            }
        }
        return null;
    }

    private static String getFloorHeatingModeNameByDeviceType(String deviceType, String key, String value) {
        ControlDataFloorHeatingBean.ControlData floorHeatingControlData = null;
        if (!TextUtils.isEmpty(deviceType)) {
            ControlDataFloorHeatingBean floorHeatingInfo = DeviceInfoParseUtil.parseData(AppApplication.getInstance(), deviceType, ControlDataFloorHeatingBean.class);
            floorHeatingControlData = floorHeatingInfo.getControlData();
        }
        List<DeviceModeValue> valueBeans = null;
        switch (key) {
            case MODE:
                if (floorHeatingControlData == null) {
                    return AppApplication.getInstance().getString(R.string.mode);
                }
                valueBeans = floorHeatingControlData.getMode();
                break;
        }
        if (valueBeans == null) {
            return null;
        }
        for (DeviceModeValue valueBean : valueBeans) {
            if ((valueBean.getValue() + "").equals(value)) {
                int nameRes = AppApplication.getInstance().getResources().getIdentifier(valueBean.getName(), "string", AppApplication.getInstance().getPackageName());
                return AppApplication.getInstance().getString(nameRes);
            }
        }
        return null;
    }

    private static String getFreshAirModeNameByDeviceType(String deviceType, String key, String value) {
        ControlDataFreshAirBean.ControlData freshAirControlData = null;
        if (!TextUtils.isEmpty(deviceType)) {
            ControlDataFreshAirBean freshAirInfo = DeviceInfoParseUtil.parseData(AppApplication.getInstance(), deviceType, ControlDataFreshAirBean.class);
            freshAirControlData = freshAirInfo.getControlData();
        }
        List<DeviceModeValue> valueBeans = null;
        switch (key) {
            case MODE:
                if (freshAirControlData == null) {
                    return AppApplication.getInstance().getString(R.string.mode);
                }
                valueBeans = freshAirControlData.getMode();
                break;
            case WIND_SPEED:
                if (freshAirControlData == null) {
                    return AppApplication.getInstance().getString(R.string.wind_speed);
                }
                valueBeans = freshAirControlData.getWindspeed();
                break;
            case EXHAUST_SPEED:
                if (freshAirControlData == null) {
                    return AppApplication.getInstance().getString(R.string.exhaust_wind);
                }
                valueBeans = freshAirControlData.getExhaustspeed();
                break;
        }
        if (valueBeans == null) {
            return null;
        }
        for (DeviceModeValue valueBean : valueBeans) {
            if ((valueBean.getValue() + "").equals(value)) {
                int nameRes = AppApplication.getInstance().getResources().getIdentifier(valueBean.getName(), "string", AppApplication.getInstance().getPackageName());
                return AppApplication.getInstance().getString(nameRes);
            }
        }
        return null;
    }

    public static String getDeviceValue(DeviceBean device) {
        if (device == null) {
            return "";
        }
//        //没有开关按钮的设备
//        if ("1".equals(device.getMainControlType()) && !device.isIsOpen()) {
//            return getString(R.string.switch_off);
//        }
        if (DeviceType.BREAKER_LIGHT.stringType.equals(device.getDeviceType())) {
            return "";
        } else if (device.getIsOpen() && (DeviceType.ADJUST_LIGHT.stringType.equals(device.getDeviceType())
                || DeviceType.TCT_DL.stringType.equals(device.getDeviceType())
                || DeviceType.ADJUST_LCT.stringType.equals(device.getDeviceType())
                || DeviceType.ADJUST_COLOR.stringType.equals(device.getDeviceType()))) {
            return device.getLight() + "%";
        } else if (device.getIsOpen() && (DeviceType.AIR_CONDITIONER.stringType.equals(device.getType())
                || DeviceType.FLOOR_HEATING.stringType.equals(device.getType())
                || DeviceType.YILIN_FLOOR_HEATING.stringType.equals(device.getDeviceType()))) {
            return device.getTemperature() + "℃";
        } else if (DeviceType.WINDOW_CURTAINS.stringType.equals(device.getDeviceType())
                || DeviceType.ROLLER_SHUTTER.stringType.equals(device.getDeviceType())
                || "oc_windowcurtains".equals(device.getDeviceType())) {
            return device.getStrokePercentage() + "%";
        } else if (device.getIsOpen() && (DeviceType.FRESHAIR.stringType.equals(device.getType()))) {
            return getFreshAirModeNameByDeviceType(device.getDeviceType(), WIND_SPEED, device.getWindSpeed() + "");
        }
        return "";
    }

    public static String getString(int res) {
        return AppApplication.getInstance().getString(res);
    }

    public static String getString(int res, String holder) {
        return AppApplication.getInstance().getString(res, holder);
    }

//    public static List<String> getValueStr(String deviceType, Map<String, String> valueMap) {
//        if (valueMap == null || valueMap.isEmpty()) return new ArrayList<>();
//        ArrayList<String> value = new ArrayList<>();
//        for (String key : valueMap.keySet()) {
//            value.add(getValueStr(deviceType, key, valueMap.get(key)));
//        }
//        return value;
//    }

    public static List<String> getValueStr_max3(String type, String deviceType, Map<String, String> valueMap, String deviceSerialNo) {
        if (valueMap == null || valueMap.isEmpty()) return new ArrayList<>();
        ArrayList<String> value = new ArrayList<>();
        int i = 0;
        if (valueMap.size() < 3) {
            for (String key : valueMap.keySet()) {
                value.add(getValueStr(type, deviceType, key, valueMap.get(key), deviceSerialNo));
            }
        } else {
            for (String key : valueMap.keySet()) {
                if (i == 2) {
                    value.add("...");
                    return value;
                }
                value.add(getValueStr(type, deviceType, key, valueMap.get(key), deviceSerialNo));
                i++;
            }
        }
        return value;
    }


    public static BaseDeviceFragment getDeviceDetailPage(DeviceBean device, int enterType) {
        BaseDeviceFragment deviceFragment = null;

        if (device.getDeviceType().equals(DeviceType.BREAKER_LIGHT.stringType)) {
            deviceFragment = SwitchFragment.newInstance();
            deviceFragment.setDevice(device.getDeviceSerialNo());
        } else if (device.getDeviceType().equals(DeviceType.ADJUST_LIGHT.stringType)) {
            deviceFragment = DimmingFragment.newInstance();
            deviceFragment.setDevice(device.getDeviceSerialNo());
        } else if (device.getDeviceType().equals(DeviceType.TCT_DL.stringType) || device.getDeviceType().equals(DeviceType.ADJUST_LCT.stringType)) {
            deviceFragment = ColorTemperatureFragment.newInstance();
            deviceFragment.setDevice(device.getDeviceSerialNo());
        }else if (device.getDeviceType().equals(DeviceType.ADJUST_COLOR.stringType)) {
            deviceFragment = ColorDeviceFragment.newInstance();
            deviceFragment.setDevice(device.getDeviceSerialNo());
        } else if (device.getDeviceType().equals(DeviceType.WINDOW_CURTAINS.stringType) || device.getDeviceType().equals("oc_windowcurtains")) {
            deviceFragment = KHCurtainFragment.newInstance();
            deviceFragment.setDevice(device.getDeviceSerialNo());
        } else if (device.getDeviceType().equals(DeviceType.ROLLER_SHUTTER.stringType)) {
            deviceFragment = RollerShutterFragment.newInstance();
            deviceFragment.setDevice(device.getDeviceSerialNo());
        } else if (device.getDeviceType().equals(DeviceType.BOFU_CURTAINS.stringType) || device.getDeviceType().equals("dream_windowcurtains")) {
            deviceFragment = BFCurtainFragment.newInstance();
            deviceFragment.setDevice(device.getDeviceSerialNo());
        } else if (device.getDeviceType().equals("blind_windowcurtains")) {
            deviceFragment = BlindCurtainFragment.newInstance();
            deviceFragment.setDevice(device.getDeviceSerialNo());
        } else if (device.getType().equals("airconditioner")) {
            if(device.getDeviceType().equals("universal_ac")){
                deviceFragment = UniversalAirFragment.newInstance();
                deviceFragment.setDevice(device.getDeviceSerialNo());
            }else{
                deviceFragment = AirConditionerFragment.newInstance();
                deviceFragment.setDevice(device.getDeviceSerialNo());
            }
        } else if (device.getType().equals("floorheating")) {
            deviceFragment = FloorHeatingFragment.newInstance();
            deviceFragment.setDevice(device.getDeviceSerialNo());
        } else if (device.getType().equals("freshair")) {
            if(device.getDeviceType().equals("universal_freshair")){
                deviceFragment = UniversalDeviceFragment.newInstance();
                deviceFragment.setDevice(device.getDeviceSerialNo());
            }else{
                deviceFragment = FreshAirFragment.newInstance();
                deviceFragment.setDevice(device.getDeviceSerialNo());
            }
        } else if (device.getType().equals("wireless_433") || device.getType().equals("wireless_315") || device.getType().equals("infrared")) {
            deviceFragment = WirelessInfraredFragment.newInstance();
            deviceFragment.setDevice(device.getDeviceSerialNo());
        } else if (device.getDeviceType().equals(DeviceType.WALL_SWITCH1.stringType)||device.getDeviceType().equals(DeviceType.WALL_SWITCH_S1.stringType)) {
            deviceFragment = OneKeyFragment.newInstance();
            deviceFragment.setDevice(device.getDeviceSerialNo());
        } else if (device.getDeviceType().equals(DeviceType.WALL_SWITCH2.stringType) || device.getDeviceType().equals(DeviceType.WALL_SWITCH_S2.stringType)) {
            deviceFragment = TwoKeyFragment.newInstance();
            deviceFragment.setDevice(device.getDeviceSerialNo());
        } else if(device.getDeviceType().equals(DeviceType.WALL_SWITCH3.stringType) || device.getDeviceType().equals(DeviceType.WALL_SWITCH_S3.stringType)){
            deviceFragment = ThreeKeyFragment.newInstance();
            deviceFragment.setDevice(device.getDeviceSerialNo());
        }else if (device.getDeviceType().equals(DeviceType.WALL_SWITCH4.stringType) || device.getDeviceType().equals(DeviceType.WALL_SWITCH_J4.stringType) || device.getDeviceType().equals(DeviceType.WALL_SWITCH_S4.stringType)) {
            deviceFragment = FourKeyFragment.newInstance();
            deviceFragment.setDevice(device.getDeviceSerialNo());
        } else if(device.getDeviceType().equals(DeviceType.WALL_SWITCH6.stringType) || device.getDeviceType().equals(DeviceType.WALL_SWITCH_S6.stringType)){
            deviceFragment = SixKeyFragment.newInstance();
            deviceFragment.setDevice(device.getDeviceSerialNo());
        } else if (device.getDeviceType().equals(DeviceType.WALL_SWITCH8.stringType) || device.getDeviceType().equals(DeviceType.WALL_SWITCH_J8.stringType)) {
            deviceFragment = EightKeyFragment.newInstance();
            deviceFragment.setDevice(device.getDeviceSerialNo());
        } else if (device.getDeviceType().equals(DeviceType.XINYUE_MUSIC.stringType)){
            deviceFragment = MusicFragment.newInstance();
            deviceFragment.setDevice(device.getDeviceSerialNo());
        } else if (device.getDeviceType().equals(DeviceType.UNIVERSAL_SEQUENCER.stringType) || device.getDeviceType().equals(DeviceType.UNIVERSAL_PROJECTOR.stringType) || device.getDeviceType().equals(DeviceType.UNIVERSAL_PLAYER.stringType) || device.getDeviceType().equals(DeviceType.UNIVERSAL_PA.stringType)){
            deviceFragment = UniversalDeviceFragment.newInstance();
            deviceFragment.setDevice(device.getDeviceSerialNo());
        }else if (device.getDeviceType().equals(DeviceType.AC_CAMERA.stringType)){
            deviceFragment = DoorBellFragment.newInstance();
            deviceFragment.setDevice(device.getDeviceSerialNo());
        }else {
            deviceFragment = NoSuchFragment.newInstance();
            deviceFragment.setDevice(device.getDeviceSerialNo());
        }
        deviceFragment.setEnterType(enterType);
        return deviceFragment;
    }

    public static String getControllerTypeName(String productModel) {
        switch (productModel) {
            case "0x0001":
            case "0x0002":
            case "0x0003":
            case "0x0004":
            case "0x0005":
            case "0x0006":
            case "0x0007":
            case "0x0008":
                return getString(R.string.light_controller);//灯光模块
            case "0x0501":
            case "0x0502":
            case "0x0503":
            case "0x0504":
            case "0x0505":
            case "0x0506":
            case "0x0507":
            case "0x0509":
            case "0x053B":
            case "0x053C":
            case "0x0540":
            case "0x0541":
            case "0x0542":
            case "0x0543":
            case "0x053E":
            case "0x053F":
            case "0x0551":
            case "0x0552":
            case "0x0553":
            case "0x0554":
            case "0x053D":
                return getString(R.string.smart_panel);//智能开关
            case "0x1001":
            case "0x1002":
            case "0x1003":
            case "0x1004":
            case "0x1005":
            case "0x1006":
            case "0x2001":
            case "0x2002":
            case "0x2003":
            case "0x2005":
            case "0x2503":
            case "0x2504":
            case "0x2505":
            case "0x2006":
                return getString(R.string.translator);//协议模块
            case "0x2501":
            case "0x2502":
            case "0x2506":
            case "0x2509":
                return getString(R.string.detector);//传感模块
            case "0x1503":
                return getString(R.string.area_gateway);//区域网关
        }
        return getString(R.string.other);//其他
    }

    public static String[] controllerCategoryArray = new String[]{
            getString(R.string.area_gateway),
            getString(R.string.light_controller),
            getString(R.string.detector),
            getString(R.string.translator),
            getString(R.string.smart_switch),
            getString(R.string.smart_panel),
            getString(R.string.other)
    };

    public static DeviceType getDeviceType(String subType) {
        for (DeviceType type : DeviceType.values()) {
            if (type.stringType.equals(subType)) {
                return type;
            }
        }
        return null;
    }

    public static String getDeviceTypeInfo(String subType, String key) {
        DeviceType deviceSubType = getDeviceType(subType);
        if (deviceSubType == null) return "";
        if (deviceSubType.info == null) return "";
        String[] values = deviceSubType.info.split("|");
        for (String k_v : values) {
            if (k_v.split(":")[0].equals(key)) {
                return k_v.split(":")[1];
            }
        }
        return "";
    }

    public static void startDeviceStatusActivity(Context context, String key, DeviceBean device, String conditionSerialNo) {
        Intent intent = null;
        switch (key) {
            case LLDeviceUtil.POWER:
                intent = new Intent(context, SwitchStatusActivity.class);
                intent.putExtra("type", SwitchStatusActivity.TYPE_POWER);
                break;
            case LLDeviceUtil.LIGHT:
                intent = new Intent(context, BrightnessSettingActivity.class);
                intent.putExtra("device_type", device.getDeviceType());
                break;
            case LLDeviceUtil.KELVIN:
                intent = new Intent(context, ColorTemperatureSettingActivity.class);
                intent.putExtra("device_type", device.getDeviceType());
                break;
            case LLDeviceUtil.STROKE_PERCENTAGE:
                intent = new Intent(context, SelectCurtainsStatusActivity.class);
                break;
            case LLDeviceUtil.MODE:
                intent = new Intent(context, SelectDeviceModeActivity.class);
                intent.putExtra("type", device.getType());
                intent.putExtra("device_type", device.getDeviceType());
                break;
            case LLDeviceUtil.WIND_SPEED:
                intent = new Intent(context, SelectDeviceWindSpeedActivity.class);
                intent.putExtra("type", device.getType());
                intent.putExtra("device_type", device.getDeviceType());
                break;
            case LLDeviceUtil.EXHAUST_SPEED:
                intent = new Intent(context, SelectDeviceExhaustWindSpeedActivity.class);
                intent.putExtra("device_type", device.getDeviceType());
                break;
            case LLDeviceUtil.WIND_DIRECTION:
                intent = new Intent(context, SelectDeviceWindDirectionActivity.class);
                intent.putExtra("type", device.getType());
                intent.putExtra("device_type", device.getDeviceType());
                break;
        }
        intent.putExtra("serial_no", conditionSerialNo);//条件唯一编码
        intent.putExtra("device_serial_no", device.getDeviceSerialNo());
        intent.putExtra("device_name", device.getName());
        intent.putExtra("device_image", device.getImageUrl());
        context.startActivity(intent);
    }

    public static void startDeviceStatusActivityNew(Context context, String key, DeviceBean device, LinkageNewBean.LinkageDeviceCondition deviceCondition,int position) {
        Intent intent = null;
        switch (key) {
            case LLDeviceUtil.POWER:
                intent = new Intent(context, SwitchStatusActivityNew.class);
                intent.putExtra("type", SwitchStatusActivity.TYPE_POWER);
                break;
            case LLDeviceUtil.LIGHT:
                intent = new Intent(context, BrightnessSettingActivityNew.class);
                intent.putExtra("device_type", device.getDeviceType());
                intent.putExtra("type", LLDeviceUtil.LIGHT);
                break;
            case LLDeviceUtil.KELVIN:
                intent = new Intent(context, BrightnessSettingActivityNew.class);
                intent.putExtra("device_type", device.getDeviceType());
                intent.putExtra("type", LLDeviceUtil.KELVIN);
                break;
            case LLDeviceUtil.STROKE_PERCENTAGE:
                intent = new Intent(context, BrightnessSettingActivityNew.class);
                intent.putExtra("device_type", device.getDeviceType());
                intent.putExtra("type", LLDeviceUtil.STROKE_PERCENTAGE);
                break;
            case LLDeviceUtil.MODE:
                intent = new Intent(context, SelectDeviceModeActivityNew.class);
                intent.putExtra("type", device.getType());
                intent.putExtra("device_type", device.getDeviceType());
                intent.putExtra("action_type",LLDeviceUtil.MODE);
                break;
            case LLDeviceUtil.WIND_SPEED:
                intent = new Intent(context, SelectDeviceModeActivityNew.class);
                intent.putExtra("type", device.getType());
                intent.putExtra("device_type", device.getDeviceType());
                intent.putExtra("action_type",LLDeviceUtil.WIND_SPEED);
                break;
            case LLDeviceUtil.EXHAUST_SPEED:
                intent = new Intent(context, SelectDeviceModeActivityNew.class);
                intent.putExtra("device_type", device.getDeviceType());
                intent.putExtra("action_type",LLDeviceUtil.EXHAUST_SPEED);
                break;
            case LLDeviceUtil.WIND_DIRECTION:
                intent = new Intent(context, SelectDeviceModeActivityNew.class);
                intent.putExtra("type", device.getType());
                intent.putExtra("device_type", device.getDeviceType());
                intent.putExtra("action_type",LLDeviceUtil.WIND_DIRECTION);
                break;
        }
        intent.putExtra("isAdd",deviceCondition == null);
        if(deviceCondition != null){
            intent.putExtra("condition", deviceCondition);
        }
        intent.putExtra("position",position);
        intent.putExtra("device_serial_no", device.getDeviceSerialNo());
        intent.putExtra("device_name", device.getName());
        intent.putExtra("device_image", device.getImageUrl());
        context.startActivity(intent);
    }


    public static String getEnviromentData(DeviceBean deviceBean) {
        if(deviceBean.getDeviceType().equals("temperature_sensor")){
            return deviceBean.getStateData().getStatus().getTemperature() + "";
        }else if(deviceBean.getDeviceType().equals("humidity_sensor")){
            return deviceBean.getStateData().getStatus().getHumidity() + "";
        }else if(deviceBean.getDeviceType().equals("illuminance_sensor")){
            return deviceBean.getStateData().getStatus().getIlluminance() + "";
        }else if(deviceBean.getDeviceType().equals("pm2_5_sensor")){
            return deviceBean.getStateData().getStatus().getPm2_5() + "";
        }else if(deviceBean.getDeviceType().equals("noise_sensor")){
            return deviceBean.getStateData().getStatus().getNoise() + "";
        }else if(deviceBean.getType().equals("trigger")){
            return getString(LLSceneLinkageUtil.getStateActionName(deviceBean.getEnvironmentType(),deviceBean.getStateData().getStatus().getTrigger()));
        }
        return "";
    }

    public static String getEnviromentUnit(DeviceBean deviceBean) {
        if(deviceBean.getDeviceType().equals("temperature_sensor")){
            return "℃";
        }else if(deviceBean.getDeviceType().equals("humidity_sensor")){
            return "%";
        }else if(deviceBean.getDeviceType().equals("illuminance_sensor")){
            return "lux";
        }else if(deviceBean.getDeviceType().equals("pm2_5_sensor")){
            return "μg/m³";
        }else if(deviceBean.getDeviceType().equals("noise_sensor")){
            return "dB";
        }else if(deviceBean.getType().equals("trigger")){
            return "";
        }
        return "";
    }
}
